<?php
/**
 * golangshop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 上海瑞璞玉数字网络科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.golangshop.com
 * =========================================================
 */

namespace addon\cashier\storeapi\controller;

use addon\printer\model\PrinterOrder;
use app\storeapi\controller\BaseStoreApi;
use addon\cashier\model\Cashier as CashierModel;

class Cashier extends BaseStoreApi
{
    /**
     * 交接班
     */
    public function changeShifts()
    {
        $res = (new CashierModel())->changeShifts($this->user_info, $this->site_id, $this->store_id);
        return $this->response($res);
    }

    /**
     * 班次数据
     */
    public function shiftsData()
    {
        $data = [
            'shifts_data' => (new CashierModel())->getShiftsData($this->site_id, $this->store_id),
            'userinfo' => [
                'username' => $this->user_info['username']
            ]
        ];
        return $this->response($this->success($data));
    }

    /**
     * 交接班记录
     */
    public function changeShiftsRecord(){
        $page = $this->params[ 'page' ] ?? 1;
        $page_size = $this->params[ 'page_size' ] ?? PAGE_LIST_ROWS;
        $uid = $this->params['uid'] ?? 0;
        $start_time = $this->params['start_time'] ?? '';
        $end_time = $this->params['end_time'] ?? '';
        if (!empty($start_time)) $start_time = strtotime($start_time);
        if (!empty($end_time)) $end_time = strtotime($end_time);

        $condition = [
            [ 'csr.site_id', '=', $this->site_id ],
            [ 'csr.store_id', '=', $this->store_id ],
        ];
        if ($uid != 0) $condition[] = ['csr.uid', '=', $uid];
        if ($start_time && $end_time) {
            $condition[] = [ 'csr.end_time', 'between', [ $start_time, $end_time ] ];
        } elseif (!$start_time && $end_time) {
            $condition[] = [ 'csr.end_time', '<=', $end_time ];
        } elseif ($start_time && !$end_time) {
            $condition[] = [ 'csr.end_time', '>=', $start_time ];
        }

        $join = [ ['user u', 'u.uid = csr.uid', 'left'] ];
        $list =  (new CashierModel())->getchangeShiftsPageList($condition, 'csr.*,u.username', 'csr.id desc', $page, $page_size, 'csr', $join);
        return $this->response($list);
    }

    /**
     * 交班打印
     * @return false|string
     */
    public function printTicket()
    {
        $record_id = $this->params['record_id'] ?? 0;

        $printer_order_model = new PrinterOrder();
        $res = $printer_order_model->printer([
            'type' => 'change_shifts',
            'site_id' => $this->site_id,
            'store_id' => $this->store_id,
            'userinfo' => $this->user_info,
            'record_id' => $record_id
        ]);
        return $this->response($res);
    }

    /**
     * 获取收银台收款设置
     * @return false|string
     */
    public function getCashierCollectMoneyConfig(){
        $res = (new CashierModel())->getCashierCollectMoneyConfig($this->site_id, $this->store_id);
        return $this->response($res);
    }

    /**
     * 收银台收款设置
     * @return false|string
     */
    public function setCashierCollectMoneyConfig(){
        $data = [
            'reduction' => $this->params['reduction'] ?? 1,
            'point' => $this->params['point'] ?? 1,
            'balance' => $this->params['balance'] ?? 1,
            'balance_safe' => $this->params['balance_safe'] ?? 0,
            'sms_verify' => $this->params['sms_verify'] ?? 0,
            'pay_type' => json_decode($this->params['pay_type'] ?? '[]', true),
        ];
        $res = (new CashierModel())->setCashierCollectMoneyConfig($this->site_id, $this->store_id, $data);
        return $this->response($res);
    }
}